/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AdminConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.Log.Level;
/*     */ import com.zimbra.common.util.LogFactory;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.accesscontrol.AdminRight;
/*     */ import com.zimbra.cs.account.accesscontrol.Rights.Admin;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.Arrays;
/*     */ import java.util.Collection;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class AddAccountLogger
/*     */   extends AdminDocumentHandler
/*     */ {
/*  48 */   static String CATEGORY_ALL = "all";
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  53 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*     */     
/*  55 */     Server localServer = Provisioning.getInstance().getLocalServer();
/*  56 */     checkRight(zsc, context, localServer, Rights.Admin.R_manageAccountLogger);
/*     */     
/*     */ 
/*  59 */     Account account = getAccountFromLoggerRequest(request);
/*     */     
/*  61 */     Element eLogger = request.getElement("logger");
/*  62 */     String category = eLogger.getAttribute("category");
/*  63 */     String sLevel = eLogger.getAttribute("level");
/*     */     
/*     */ 
/*  66 */     Log.Level level = null;
/*     */     try {
/*  68 */       level = Log.Level.valueOf(sLevel.toLowerCase());
/*     */     } catch (IllegalArgumentException e) {
/*  70 */       String error = String.format("Invalid level: %s.  Valid values are %s.", new Object[] { sLevel, StringUtil.join(",", Log.Level.values()) });
/*     */       
/*  72 */       throw ServiceException.INVALID_REQUEST(error, null);
/*     */     }
/*     */     
/*     */     Collection<Log> loggers;
/*     */     Collection<Log> loggers;
/*  77 */     if (category.equalsIgnoreCase(CATEGORY_ALL)) {
/*  78 */       loggers = LogFactory.getAllLoggers();
/*     */     } else {
/*  80 */       if (!LogFactory.logExists(category)) {
/*  81 */         throw ServiceException.INVALID_REQUEST("Log category " + category + " does not exist.", null);
/*     */       }
/*  83 */       loggers = Arrays.asList(new Log[] { LogFactory.getLog(category) });
/*     */     }
/*     */     
/*     */ 
/*  87 */     Element response = zsc.createElement(AdminConstants.ADD_ACCOUNT_LOGGER_RESPONSE);
/*  88 */     for (Log log : loggers) {
/*  89 */       ZimbraLog.misc.info("Adding custom logger: account=%s, category=%s, level=%s", new Object[] { account.getName(), category, level });
/*     */       
/*  91 */       log.addAccountLogger(account.getName(), level);
/*  92 */       response.addElement("logger").addAttribute("category", log.getCategory()).addAttribute("level", level.name());
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*  97 */     return response;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   static Account getAccountFromLoggerRequest(Element request)
/*     */     throws ServiceException
/*     */   {
/* 106 */     Account account = null;
/* 107 */     Provisioning prov = Provisioning.getInstance();
/* 108 */     Element idElement = request.getOptionalElement("id");
/*     */     
/* 110 */     if (idElement != null)
/*     */     {
/* 112 */       ZimbraLog.soap.info("The <%s> element is deprecated for <%s>.  Use <%s> instead.", new Object[] { "id", request.getName(), "account" });
/*     */       
/* 114 */       String id = idElement.getText();
/* 115 */       account = prov.get(Key.AccountBy.id, id);
/* 116 */       if (account == null) {
/* 117 */         throw AccountServiceException.NO_SUCH_ACCOUNT(idElement.getText());
/*     */       }
/*     */     }
/*     */     else {
/* 121 */       Element accountElement = request.getElement("account");
/* 122 */       Key.AccountBy by = Key.AccountBy.fromString(accountElement.getAttribute("by"));
/* 123 */       account = prov.get(by, accountElement.getText());
/* 124 */       if (account == null) {
/* 125 */         throw AccountServiceException.NO_SUCH_ACCOUNT(accountElement.getText());
/*     */       }
/*     */     }
/* 128 */     return account;
/*     */   }
/*     */   
/*     */   public void docRights(List<AdminRight> relatedRights, List<String> notes)
/*     */   {
/* 133 */     relatedRights.add(Rights.Admin.R_manageAccountLogger);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/AddAccountLogger.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */